package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;


    @Override
    public User login(String phone, String code, String codeInSession) {

        if (!(codeInSession != null && code.equalsIgnoreCase(codeInSession))) {
            throw new BusinessException("验证码错误");
        }
       User user = userMapper.findUserBYPhone(phone);
        if (user != null) {
            if (user.getStatus() == 1) {
                throw new BusinessException("您的手机号已被禁用");
            }
        } else {
            user = new User();
            user.setPhone(phone);
            user.setStatus(0);
            userMapper.save(user);
        }
        return user;
    }
}
